#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDebug>
#include <QtMath>
#include <QFontMetrics>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , m_petPos(200, 200)
    , m_petSize(100, 100)
    , m_mood(50)
    , m_isHappy(false)
    , m_petAnimationCounter(0)
{
    ui->setupUi(this);

    setWindowTitle("Shit Man!");
    resize(600, 600);

    // 创建定时器对象
    m_lifeTimer = new QTimer(this);
    connect(m_lifeTimer, &QTimer::timeout, [=]{
        m_mood -= 1;
        if(m_mood < 0){
            m_mood = 0;
        }

        // 如果处于开心状态
        if(m_isHappy){
            m_petAnimationCounter++;
            if(m_petAnimationCounter > 10){
                m_isHappy = false;
                m_petAnimationCounter = 0;
            }
        }
        update();   // 重绘界面
    });
    m_lifeTimer->start(1000);   // 定时器为1s
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    // 创建绘制器
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    painter.fillRect(rect(), QColor(240, 240, 255));

    // 计算动画效果大小
    int currentSize = m_petSize.width();
    if(m_isHappy) {
        currentSize += qSin(m_petAnimationCounter * 0.3) * 5;
    }

    // 绘制宠物
    QColor petColor = QColor::fromHsv(m_mood * 1.2, 255, 255);
    painter.setBrush(petColor);
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(m_petPos, currentSize/2, currentSize/2);

    // 绘制眼睛
    painter.setBrush(Qt::white);
    painter.drawEllipse(QPoint(m_petPos.x() - 15, m_petPos.y() - 10), 8, 8);
    painter.drawEllipse(QPoint(m_petPos.x() + 15, m_petPos.y() - 10), 8, 8);

    // 绘制瞳孔
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(m_petPos.x() - 15, m_petPos.y() - 10), 3, 3);
    painter.drawEllipse(QPoint(m_petPos.x() + 15, m_petPos.y() - 10), 3, 3);

    // 绘制嘴巴
    QPainterPath mouth;
    if(m_mood > 50) {
        // 开心表情
        mouth.moveTo(m_petPos.x() - 15, m_petPos.y() + 15);
        mouth.quadTo(m_petPos.x(), m_petPos.y() + 25,
                     m_petPos.x() + 15, m_petPos.y() + 15);
    } else {
        // 不开心表情
        mouth.moveTo(m_petPos.x() - 15, m_petPos.y() + 20);
        mouth.quadTo(m_petPos.x(), m_petPos.y() + 10,
                     m_petPos.x() + 15, m_petPos.y() + 20);
    }
    painter.setPen(QPen(Qt::black, 2));
    painter.setBrush(Qt::NoBrush);
    painter.drawPath(mouth);

    // 绘制心情状态
    painter.setPen(Qt::black);
    painter.drawText(20, 50, QString("心情值: %1").arg(m_mood));

    // 绘制进度条
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(200, 200, 200));
    painter.drawRect(100, 20, 200, 10);

    painter.setBrush(petColor);
    painter.drawRect(100, 20, m_mood * 2, 10);

    // 绘制提示
    if(m_mood < 35) {
        painter.setPen(Qt::red);
        QFontMetrics metrics(painter.font());
        int textWidth = metrics.horizontalAdvance("Fuck you! 不摸？");
        painter.drawText(width()/2 - textWidth/2, 60, "Fuck you! 不摸？");
    }

    // 绘制互动提示
    painter.setPen(QColor(100, 100, 100));
    painter.drawText(10, height() - 10, "鼠标移动或点击宠物与它互动");
}

// 鼠标移动事件处理
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    // 计算宠物矩形区域
    QRect petRect(m_petPos.x() - m_petSize.width() / 2,
                  m_petPos.y() - m_petSize.height() / 2,
                  m_petSize.width(), m_petSize.height()
                  );

    // 如果鼠标在宠物区域内
    if(petRect.contains(event->pos())){
        m_mood += 5;
        if(m_mood > 100){
            m_mood = 100;
        }
        m_isHappy = true;
        update();
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    // 计算宠物矩形区域
    QRect petRect(m_petPos.x() - m_petSize.width() / 2,
                  m_petPos.y() - m_petSize.height() / 2,
                  m_petSize.width(), m_petSize.height()
                  );

    if(petRect.contains(event->pos()) && event->button() == Qt::LeftButton){
        QMessageBox::information(this, "互动", "AUV，摸你爹呢");
        m_mood += 10;
        if(m_mood > 100){
            m_mood = 100;
        }
        m_isHappy = true;
        update();
    }
}



